package com.itheima.app.controller;

import com.itheima.app.manager.TestSoulManager;
import com.itheima.domain.db.Answers;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class TestSoulController {
    @Autowired
    TestSoulManager testSoulManager;

    @GetMapping("/testSoul")
    public ResponseEntity testSoulList() {
        return testSoulManager.testSoulList();
    }

    @PostMapping("/testSoul")
    public ResponseEntity submitSoulTest(@RequestBody Map<String, Answers[]> map) {
        List<Long> questions = new ArrayList<>();
        List<Long> options = new ArrayList<>();
        for (Answers answers : map.get("answers")) {
            questions.add(Long.valueOf(answers.getQuestionId()));
            options.add(Long.valueOf(answers.getOptionId()));
        }
        return testSoulManager.submitSoulTest(questions, options);
    }

    @GetMapping("/testSoul/report/{reportId}")
    public ResponseEntity findReport(@PathVariable Long reportId) {
        return testSoulManager.findReport(reportId);
    }

}
